package spikes

import (
	"enjoy_xxz/formdata"
	"enjoy_xxz/internal/model/database/mysql"
	"gorm.io/gorm"
	"time"
)

// 秒杀活动表
type SpikesActivity struct {
	gorm.Model
	SpikesTitle    string    `gorm:"type:varchar(30);comment:活动标题;"`
	SpikeStartTime time.Time `gorm:"type:datetime(0);comment:秒杀开始时间;"`
	SpikeEndTime   time.Time `gorm:"type:datetime(0);comment:秒杀结束时间;"`
	SpikesStatus   int32     `gorm:"type:tinyint(1);default:0;comment:秒杀活动状态 0活动结束 1活动开启;"`
}

// 获取秒杀活动列表方法
func (s *SpikesActivity) GetSpokesActivityList() (lists []formdata.ActivityList, err error) {
	var list []SpikesActivity
	return lists, mysql.WithMysql(func(db *gorm.DB) error {
		err = db.Model(s).Find(&list).Error
		for _, val := range list {
			// 格式化时间为字符串
			startTime := val.SpikeStartTime.Format("2006-01-02 15:04:05") // 根据需要调整格式
			endTime := val.SpikeEndTime.Format("2006-01-02 15:04:05")     // 根据需要调整格式
			lists = append(lists, formdata.ActivityList{
				SpikesActivityID: int64(val.ID),
				SpikesTitle:      val.SpikesTitle,
				SpikeStartTime:   startTime,
				SpikeEndTime:     endTime,
				SpikeStatus:      val.SpikesStatus,
			})
		}
		return nil
	})
}

// 查询秒杀活动方法
func (s *SpikesActivity) GetSpokesBySpikesID() error {
	return mysql.WithMysql(func(db *gorm.DB) error {
		return db.Model(s).Where("id = ?", s.ID).First(s).Error
	})
}
